﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
using System.Device.Location;

namespace PushpinClusterer.Example
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            var doc = XDocument.Load("PushpinClusterer.Example;component/jugglingclubs.xml");

            var pins = doc.Descendants("club")
                // get clubs with coordinates
                          .Where(club => club.Descendants("coordinates").Any())
                          .Select(el => new
                          {
                              lat = double.Parse(el.Descendants("latitude").Single().Value),
                              lon = double.Parse(el.Descendants("longitude").Single().Value),
                              name = el.Descendants("name").Single().Value,
                              country = el.Descendants("country").Single().Value,
                              //address = el.Descendants("address").Single().Value
                          })
                // verify the coordinates are sensible!
                          .Where(coord => coord.lat < 90 && coord.lat > -90 && coord.lon < 90 && coord.lon > -90)
                // create a pushpin for each club
                          .Select(i => new MyGeoObject()
                          {
                              Coordinate = new GeoCoordinate(i.lat, i.lon),
                              Name = i.name,
                              Country = i.country,
                              //Address = i.address
                          }).ToList();

            var clusterer = new PushpinClusterer<MyGeoObject>(map1, pins, 50);
            pushPinModelsLayer.DataContext = clusterer; 
        }
    }
}